package qsh.laputa.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import qsh.laputa.entity.Account;
import qsh.laputa.entity.Role;
import qsh.laputa.service.AccountService;
import qsh.laputa.service.ResourceService;
import qsh.laputa.service.RoleService;
import qsh.laputa.util.MyQuery;
import qsh.laputa.util.QueryUtil;
import qsh.laputa.util.ResultUtil;
import qsh.laputa.vo.TreeVO;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Qsh
 * @since 2021-02-22
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private AccountService accountService;

    /**
     * 进入列表页
     *
     * @return
     */
    @GetMapping("toList")
    public String toList() {
        return "role/roleList";
    }

    /**
     * 查询
     *
     * @return
     */
    @ResponseBody
    @GetMapping("list")
    public R<Map<String, Object>> list(@RequestParam Map<String, String> params) {
        MyQuery<Role> myQuery = QueryUtil.buildMyQuery(params);

        Page<Role> myPage = roleService.page(myQuery.getPage(),
                myQuery.getWrapper().orderByDesc("role_id"));

        return ResultUtil.buildPageR(myPage);
    }

    /**
     * 进入新增页
     *
     * @return
     */
    @GetMapping("toAdd")
    public String toAdd() {
        return "role/roleAdd";
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public R<Object> add(@RequestBody Role role) {
        return ResultUtil.buildR(roleService.saveRole(role));
    }

    /**
     * 进入修改页
     *
     * @return
     */
    @GetMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable Long id, Model model) {
        Role role = roleService.getById(id);
        model.addAttribute(role);
        return "role/roleUpdate";
    }

    /**
     * 修改账户
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public R<Object> update(@RequestBody Role role) {
        return ResultUtil.buildR(roleService.updateRole(role));
    }

    /**
     * 删除角色
     *
     * @return
     */
    @ResponseBody
    @DeleteMapping("toDelete/{id}")
    public R<Object> toDelete(@PathVariable Long id) {
        Integer count = accountService.getAccountCountsByRoleId(id);
        if (count > 0) {
            return R.failed("有账号正拥有该角色");
        }
        Role role = new Role();
        role.setRoleId(id);
        return ResultUtil.buildR(roleService.removeByIdWithFill(role));
    }

    /**
     * 进入详情页
     *
     * @return
     */
    @GetMapping("toDetail/{id}")
    public String toDetail(@PathVariable Long id, Model model) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return "role/roleDetail";
    }

    @ResponseBody
    @GetMapping({"listResource", "listResource/{roleId}", "listResource/{roleId}/{flag}"})
    public R<List<TreeVO>> listResource(@PathVariable(required = false) Long roleId,
                                        @PathVariable(required = false) Integer flag) {
        return R.ok(resourceService.listResource(roleId, flag));
    }

}
